﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.ThucThe;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.Common;

namespace TracNghiemTinHocDaiCuong
{
    public partial class QuanLyCauHoi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<CauHoi> danhSachCauHoi = CauHoiBIZ.LayDanhSach();
            BindDuLieu_HienThiThongBao(danhSachCauHoi);
        }

        private void BindDuLieu_HienThiThongBao(IList<CauHoi> danhSachCauHoi)
        {
            if (danhSachCauHoi == null)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị";
            else
                lblThongBao.Text = "";
            gvDanhSachCauHoi.DataSource = danhSachCauHoi;
            gvDanhSachCauHoi.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            IList<CauHoi> danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text, hdfFilter.Value);
            BindDuLieu_HienThiThongBao(danhSachCauHoi);
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtNoiDung.Text = "";
        }

        protected void ClickFilter(object sender, CommandEventArgs e)
        {
            hdfFilter.Value = e.CommandName;
            LocDanhSachCauHoi(e.CommandName);
        }

        private void LocDanhSachCauHoi(string phamVi)
        {
            IList<CauHoi> danhSachCauHoi = new List<CauHoi>();
            switch (phamVi)
            {
                case "All":
                    {
                        danhSachCauHoi = CauHoiBIZ.LayDanhSach();
                        break;
                    }
                case "09":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[0-9]%");
                        break;
                    }
                case "AE":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[A-E]%");
                        break;
                    }
                case "FJ":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[F-J]%");
                        break;
                    }
                case "KN":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[K-N]%");
                        break;
                    }
                case "OR":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[O-R]%");
                        break;
                    }
                case "SV":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[S-V]%");
                        break;
                    }
                case "WZ":
                    {
                        danhSachCauHoi = CauHoiBIZ.TimKiem(txtNoiDung.Text,
                            "[W-Z]%");
                        break;
                    }
            }
            BindDuLieu_HienThiThongBao(danhSachCauHoi);
        }

        protected void btnThemCauHoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemCauHoi.aspx");
        }

        protected void btnSuaCauHoi_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("SuaCauHoi.aspx?ID={0}", ViewState["idCauHoi"]));
        }

        protected void btnXoaCauHoi_Click(object sender, EventArgs e)
        {
            CauHoiBIZ.Xoa(Methods.ConvertNumber(ViewState["idCauHoi"].ToString()));
            LayDanhSach();
        }

        private void LayDanhSach()
        {
            gvDanhSachCauHoi.DataSource = CauHoiBIZ.LayDanhSach();
            gvDanhSachCauHoi.DataBind();
        }

        protected void lkbtnCauHoi_Click(object sender, CommandEventArgs e)
        {
            ViewState["idCauHoi"] = Methods.ConvertNumber(e.CommandName);
        }

        protected void gvDanhSachCauHoi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachCauHoi.PageIndex = e.NewPageIndex;
            gvDanhSachCauHoi.DataBind();
        }
    }
}